<template>
  <view class="container">
    <!-- 状态栏占位 -->
    <view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
    
    <!-- 头部搜索区 -->
    <view class="header">
      <view class="search-box" @click="toSearch">
        <i class="fa fa-search" style="font-size: 18px; color: #999;"></i>
        <text class="search-placeholder">搜索工具...</text>
      </view>
    </view>
    
    <!-- 分类标签 -->
    <scroll-view class="category-scroll" scroll-x>
      <view class="category-container">
        <view 
          v-for="(category, index) in categories" 
          :key="index"
          class="category-item"
          :class="{ active: activeCategory === category.id }"
          @click="activeCategory = category.id"
        >
          <text>{{ category.name }}</text>
        </view>
      </view>
    </scroll-view>
    
    <!-- 工具列表 -->
    <view class="tools-container">
      <view 
        class="tool-grid"
        v-if="filteredTools.length > 0"
      >
        <view 
          class="tool-card" 
          v-for="tool in filteredTools" 
          :key="tool.id"
          @click="navigateToTool(tool)"
          :style="{ backgroundColor: tool.color + '10' }"
        >
          <view class="tool-icon" :style="{ backgroundColor: tool.color }">
            <i v-if="tool.iconInfo.icon_type=='fa'" :class="tool.iconInfo.icon" style="font-size: 24px; color: white;"></i>
			<uni-icons v-else="tools.icon_type!=='fa'" type="spinner-cycle" size="30"></uni-icons>
          </view>
          <text class="tool-name">{{ tool.name }}</text>
        </view>
      </view>
      
      <view class="empty-state" v-else>
        <i class="fa fa-ban" style="font-size: 60px; color: #ccc;"></i>
        <text class="empty-text">没有找到相关工具</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
// 状态栏高度
const statusBarHeight = ref(0)

// 分类数据
const categories = ref([
  { id: 'all', name: '全部' },
  { id: 'daily', name: '日常' },
  { id: 'tools', name: '工具' },
  { id: 'system', name: '系统' },
  { id: 'study', name: '学习' }
])

// 选中的分类
const activeCategory = ref('all')

// 工具数据（替代图标）
const tools = ref([
  {
    id: 1,
    is_finish: true,
    name: '计算器',
    iconInfo: {
      icon: 'fa fa-calculator',
      icon_type: 'fa'
    },
    color: '#42b983',
    category: 'tools',
    path: '/pages/tools/calculator'
  },
  {
    id: 2,
    is_finish: false,
    name: '备忘录',
    iconInfo: {
      icon: 'fa fa-sticky-note',
      icon_type: 'fa'
    },
    color: '#3498db',
    category: 'daily',
    path: '/pages/tools/notes'
  },
  {
    id: 3,
    is_finish: false,
    name: '时钟',
    iconInfo: {
      icon: 'fa fa-qrcode',
      icon_type: ''
    },
    color: '#e74c3c',
    category: 'daily',
    path: '/pages/tools/clock'
  },
  {
    id: 4,
    is_finish: false,
    name: '二维码',
    iconInfo: {
      icon: 'fa fa-qrcode',
      icon_type: 'fa'
    },
    color: '#f39c12',
    category: 'tools',
    path: '/pages/tools/qrcode'
  },
  {
    id: 5,
    is_finish: false,
    name: '手电筒',
    iconInfo: {
      icon: 'fa fa-lightbulb',
      icon_type: ''
    },
    color: '#9b59b6',
    category: 'system',
    path: '/pages/tools/flashlight'
  },
  {
    id: 6,
    is_finish: false,
    name: '天气',
    iconInfo: {
      icon: 'fa fa-sun',
      icon_type: ''
    },
    color: '#1abc9c',
    category: 'daily',
    path: '/pages/tools/weather'
  },
  {
    id: 7,
    is_finish: false,
    name: '翻译',
    iconInfo: {
      icon: 'fa fa-language',
      icon_type: 'fa'
    },
    color: '#34495e',
    category: 'study',
    path: '/pages/tools/translate'
  },
  {
    id: 8,
    is_finish: false,
    name: '单位转换',
    iconInfo: {
      icon: 'fa fa-exchange-alt',
      icon_type: ''
    },
    color: '#8e44ad',
    category: 'tools',
    path: '/pages/tools/converter'
  },
  {
    id: 9,
    is_finish: false,
    name: '电池信息',
    iconInfo: {
      icon: 'fa fa-battery-full',
      icon_type: 'fa'
    },
    color: '#27ae60',
    category: 'system',
    path: '/pages/tools/battery'
  },
  {
    id: 10,
    is_finish: false,
    name: '文件管理',
    iconInfo: {
      icon: 'fa fa-folder',
      icon_type: 'fa'
    },
    color: '#d35400',
    category: 'system',
    path: '/pages/tools/files'
  },
  {
    id: 11,
    is_finish: false,
    name: '待办事项',
    iconInfo: {
      icon: 'fa fa-check-square',
      icon_type: 'fa'
    },
    color: '#2980b9',
    category: 'daily',
    path: '/pages/tools/todos'
  },
  {
    id: 12,
    is_finish: false,
    name: '指南针',
    iconInfo: {
      icon: 'fa fa-compass',
      icon_type: 'fa'
    },
    color: '#c0392b',
    category: 'tools',
    path: '/pages/tools/compass'
  }
])

// 过滤后的工具列表
const filteredTools = computed(() => {
  if (activeCategory.value === 'all') {
    return tools.value
  }
  return tools.value.filter(tool => tool.category === activeCategory.value)
})

// 生命周期钩子
onMounted(() => {
  // 获取状态栏高度
  statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
})

// 导航到搜索页
const toSearch = () => {
  uni.navigateTo({url:'/pages/search/search'})
}

// 导航到工具页面
const navigateToTool = (tool) => {
	if(!tool.is_finish) {
		uni.showModal({ title: '提示',
			content: '功能待开发',
		 });
	return false;
	}
  // 构造参数字符串
  const query = `id=${tool.id}&name=${encodeURIComponent(tool.name)}&icon=${encodeURIComponent(tool.icon)}&color=${encodeURIComponent(tool.color)}`;

  uni.navigateTo({
    url: `${tool.path}?${query}`
  });
};
</script>

<style scoped>
.container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.status-bar {
  width: 100%;
}

.header {
  padding: 16px;
  background-color: #fff;
}

.search-box {
  display: flex;
  align-items: center;
  background-color: #f5f7fa;
  border-radius: 24px;
  padding: 10px 16px;
}

.search-placeholder {
  margin-left: 8px;
  color: #999;
  font-size: 14px;
  flex: 1;
}

.category-scroll {
  width: 100%;
  background-color: #fff;
  border-bottom: 1px solid #f0f0f0;
  white-space: nowrap;
}

.category-container {
  display: inline-flex;
  padding: 0 16px;
}

.category-item {
  padding: 14px 16px;
  margin-right: 8px;
  font-size: 15px;
  color: #666;
  position: relative;
}

.category-item.active {
  color: #3498db;
  font-weight: 500;
}

.category-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background-color: #3498db;
  border-radius: 3px 3px 0 0;
}

.tools-container {
  padding: 16px;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

.tool-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px 8px;
  border-radius: 12px;
  transition: all 0.2s ease;
}

.tool-card:active {
  transform: scale(0.95);
  opacity: 0.8;
}

.tool-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

.tool-name {
  font-size: 13px;
  color: #333;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
}

.empty-text {
  color: #999;
  margin-top: 16px;
  font-size: 15px;
}
</style>
